package com.hyf.serviceedu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyf.commonutils.JwtUtils;
import com.hyf.commonutils.fruit;
import com.hyf.commonutils.vo.CourseWebVoOrder;
import com.hyf.serviceedu.client.OrderClient;
import com.hyf.serviceedu.entity.EduCourse;
import com.hyf.serviceedu.entity.chapter.chapterOneVo;
import com.hyf.serviceedu.entity.vo.CourseQueryVo;
import com.hyf.serviceedu.entity.vo.CourseWebVo;
import com.hyf.serviceedu.service.EduChapterService;
import com.hyf.serviceedu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(description="前端课程接口")
@RestController
@RequestMapping("/serviceedu/course")
//@CrossOrigin // 用于解决跨域问题
public class CourseQuerycontroller {
	
	@Autowired
	private EduCourseService eduCourseService;// 注入 service课程管理
	
	@Autowired// 课程详情信息
	private EduChapterService chapterService;
	
	@Autowired
	private OrderClient orderClient;// 远程调用订单服务
	
	// 条件查询带分页
	@ApiOperation(value = "分页课程列表")
	@PostMapping(value = "getfrontCourseList/{page}/{limit}")
	public fruit getfrontCourseList(
		@ApiParam(name = "page", value = "当前页码", required = true)
		@PathVariable Long page,
		@ApiParam(name = "limit", value = "每页记录数", required = true)
		@PathVariable Long limit,
		@ApiParam(name = "courseQuery", value = "查询对象", required = false)
		@RequestBody(required = false) CourseQueryVo courseQuery){
		
		Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
		
		Map<String, Object> map = eduCourseService.getCoursefronlist(pageParam, courseQuery);
		
		return  fruit.ok().data(map);
		
	}
	
	
	
	

	
	@ApiOperation(value = "根据ID查询课程")
	@GetMapping(value = "getFrontCourseInfo/{courseId}")
	public fruit getFrontCourseInfo(
			HttpServletRequest request,
			@ApiParam(name = "courseId", value = "课程ID", required = true)
		    @PathVariable String courseId
		){
	
		//查询课程信息和讲师信息,根据课程id，编写sql语句查询课程信息
		CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);
	
		//查询当前课程的章节信息
		List<chapterOneVo> chapterVoList = chapterService.getChapterchapterTwoCourseId(courseId);
		// 根据课程id和详情id查询是否支付过了
		//远程调用，判断课程是否被购买
		
		if(request!=null){
			boolean buyCourse = orderClient.isBuyCourse(courseId,JwtUtils.getMemberIdByJwtToken(request));
			System.out.println("buycoures======="+buyCourse);
			System.out.println("courseId:"+courseId);
			System.out.println(JwtUtils.getMemberIdByJwtToken(request));
			
			//返回到页面进行显示一下
			return fruit.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isBuy", buyCourse);
		}else{
		return fruit.error().message("用户id值为空请登录...");
		}
		
	
	
	}
	
	
	// 根据课程id查询课程信息
	@PostMapping("getCourseInfoOrder/{id}")
	public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
		
		CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(id);
	
		CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
		
		BeanUtils.copyProperties(courseWebVo, courseWebVoOrder);
		
		return courseWebVoOrder;
	}
	
	
}
